import { Card, Button, Form, Input, message } from "antd";
import './index.scss'
import { useDispatch } from 'react-redux'
import { fetchLogin } from "@/store/modules/user";
import { useNavigate } from "react-router-dom";

const Login = () => {
    const navigate = useNavigate()
    const dispatch = useDispatch()
    const onFinish = async (values) => {
        await dispatch(fetchLogin(values))
        navigate('/')
        message.success('登陆成功')
        console.log("Success:", values);
    };
    const onFinishFailed = (errorInfo) => {
        console.log("Failed:", errorInfo);
    };
    return (
        <div className="login-box">
            <Card title="登录" bordered={true} style={{ width: 400 }}>
                <Form
                    validateTrigger="onBlur"
                    name="basic"
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off">
                    <Form.Item
                        name="mobile"
                        rules={[
                            {
                                required: true,
                                message: "请输入手机号!",
                            },
                            {
                                pattern: /^1[3-9]\d{9}$/,
                                message: "请输入正确格式的手机号!",
                            }
                        ]}>
                        <Input placeholder="请输入手机号" size="large" />
                    </Form.Item>

                    <Form.Item
                        name="code"
                        rules={[
                            {
                                required: true,
                                message: "请输入验证码!",
                            },
                        ]}>
                        <Input placeholder="请输入验证码" size="large" />
                    </Form.Item>

                    <Form.Item
                        wrapperCol={{
                            offset: 10,
                            span: 16,
                        }}>
                        <Button type="primary" htmlType="submit">
                            确认
                        </Button>
                    </Form.Item>
                </Form>
            </Card>
        </div>
    );
};

export default Login;
